﻿
var globalDisplayMap = {
    companyID: null,
    companiesList: [],
    companiesDataSource: [],
    busRouteID: null,
    busStopsList: [],
    busRoutesDataSource: [],
    busRoutesList: []
}

var globalGenerateScripts = {
    index: -1,
    markerList: [],
    routes: null,
    directionsDisplay: null,
    directionsService: null
};

var globalMasterPage = {
    trackDataLastLocationList: [],
    logoCompany: 'DefaultLogo.png',
    intervalGPSImei: null
};

var globalClientsRouteHistory = {
    companyIDSelected: -1,
    usernameSelected: null,
    gpsImeiSelected: null,
    dateSelected: null,
    companiesList: [],
    companiesDataSource: [],
    clientsProcessList: [],
    clientsProcessDataSource: [],
    clientsProcessIDSelected: -1,
    clientsToVisitList: [],
    positionOrigin: null,
    //Useless
    dateSelected: null,
    polygonoRouteMarkers: [],
    routesList: [],
    temporalyList: [],
    response: null,
    originPoint: null,
    destinationPoint: null,
    routes: [],
    directionsService: null,
    directionsDisplay: null,
    route: null,
    firstRoute: true,
    temporatlyInterval: null,
    temporalyIndex: -1,
    timeToCalculateRoute: 500,
    trackDataByImei: [],
    GPSImeiRoute: null,
    currentSlideRouteNumber: 0,
    speedRouteHistory: 1000,
    slideInterval: null,
    direction: null,
    peopleTotalIn: 0,
    peopleTotalOut: 0,
    totalTrack: 0,
    totalKm: 0,
    direction: null,
    geocoder: null
}

var globalBusStopsRouteHistory = {
    companiesList: [],
    companiesDataSource: [],
    companyIDSelected: -1,
    vpcProcessList: [],
    vpcProcessDataSource: [],
    vpcProcessIDSelected: -1,
    busRouteIDSelected: -1,
    numControlSelected: -1,
    dateSelected: null,
    busStopsList: [],
    polygonoBusRouteMarkers: [],
    busRoutesList: [],
    temporalyList: [],
    response: null,
    originPoint: null,
    destinationPoint: null,
    routes: [],
    directionsService: null,
    directionsDisplay: null,
    route: null,
    firstBusRoute: true,
    temporatlyInterval: null,
    temporalyIndex: -1,
    timeToCalculateRoute: 500,
    trackDataByImei: [],
    GPSImeiRoute: null,
    currentSlideRouteNumber: 0,
    speedRouteHistory: 1000,
    slideInterval: null,
    direction: null,
    peopleTotalIn: 0,
    peopleTotalOut: 0,
    totalTrack: 0,
    totalKm: 0,
    direction: null,
    geocoder: null
}

var globalGeoFences = {
    companiesList: [],
    companiesDataSource: [],
    companyID: -1,
    geoFencesList: [],
    geoFencesDataSource: [],
    geoFenceIDSelected: -1,
    latitudePolygonoList: [],
    longitudePolygonoList: []
};

var globalNotificationHistoryByDates = {
    notificationsList: [],
    notificationsDataSource: []
};

var globalNotificationManager = {
    companiesDataSource: [],
    companiesList: [],
    companyIDSelected: -1,
    notificationsList: [],
    notificationsDataSource: [],
    notificationIDSelected: -1,
    geoFencesList: [],
    geoFencesDataSource: [],
    geoFenceIDSelected: -1,
    gpsGroupList: [],
    gpsGroupDataSource: [],
    gpsGroupIDSelected: -1,
    gpsStockList: [],
    gpsStockDataSource: [],
    gpsImeiSelected: -1,
    lastNotyGeoFenceToSendAdmin: false,
    currentNotyGeoFenceToSendAdmin: false,
    isUpdateNotyGeoFenceToSendAdmin: false,
    lastNotyGPSImeiToSendUser: false,
    currentNotyGPSImeiToSendUser: false,
    isUpdateNotyGPSImeiToSendUser: false,
    gpsImeiListToInsert: [],
    gpsImeiListToDelete: [],
    widthForMsg: 300,
    usersGroupsList: [],
    usersGroupsDataSource: [],
    usersList: [],
    usersDataSource: [],
    gpsImeiList: [],
    gpsImeiDataSource: [],
    departmentsList: [],
    departmentIDSelected: -1,
    departmentsDataSource: []
};

var globalClientManager = {
    companiesDataSource: [],
    companiesList: [],
    companyID: -1,
    clientID: null,
    clientsList: [],
    clientsDataSource: [],
    clientMarker: null,
    clientGroupID: -1,
    clientsGroupsDataSource: [],
    clientsGroupsList: []
};

var globalBusStopsManager = {
    companiesDataSource: [],
    companiesList: [],
    companyID: -1,
    busStopID:-1,
    busStopsList: [],
    busStopsDataSource: [],
    busStopMarker: null,
    busRouteID: -1,
    busRoutesDataSource: [],
    busRoutesList: [],
    polygonoBusRouteMarkers: [],
    busRoutes: [],
    response: null,
    originPoint: null,
    destinationPoint: null,
    routes: [],
    directionsService: null,
    directionsDisplay: null,
    route: null,
    firstBusRoute: true,
    temporalyList: [],
    temporatlyInterval: null,
    temporalyIndex: -1,
    timeToCalculateRoute: 500
}

var globalBusRoutesManager = {
    companiesDataSource: [],
    companiesList: [],
    companyID: -1,
    busStopID: -1,
    busStopsList: [],
    busStopsDataSource: [], //busRouteDetailMarker: null,
    busRouteID: -1,
    busRoutesDataSource: [],
    busRoutesList: [],
    polygonoBusRouteMarkers: [],
    busRoutes: [],
    response: null,
    originPoint: null,
    destinationPoint: null,
    routes: [],
    directionsService: null,
    directionsDisplay: null,
    route: null,
    firstBusRoute: true,
    isValidClickPoint: true,
    //isValidDragEndPoint: true,
    isClick: false,
    isSeletedItemBusRoute: false,
    temporalyList: [],
    temporatlyInterval: null,
    temporalyIndex: -1,
    timeToCalculateRoute: 500
}

var globalGPSImei = {
    imeiSelected: -1
};

var globalList = {
    trackDataByImei: [],
    geoFence: [],//no se utiliza, borrar al final
    stopsMarkers: [],
    peopleCounterMarkers: []
};

var globalRoutesManager = {
    usersList: [],
    usersDataSource: [],
    usernameSelected: -1,
    allClientsList: [],
    allClientsDataSource: [],
    allClientGroupsDataSource: [],
    allClientsSelectedList: [],
    allClientsSelectedDataSource: [],
    gpsImeiList: [],
    gpsImeiDataSource: [],
    gpsImeiSelected: -1,
    companiesList: [],
    companiesDataSource: [],
    companyIDSelected: -1,
    departmentsList: [],
    departmentsDataSource: [],
    departmentIDSelected: -1,
    usersGroupsList: [],
    usersGroupsDataSource: [],
    userGroupIDSelected: -1
};

var globalRoutes = {
    isCenterRoute: true,
    isCenterGPSImei: true,
    direction: null,
    refreshIntervalId: null,
    currentGpsPoint: 0,
    currentIndexRoute: -1,
    currentIndexNearClient: -1,
    numPos: 0,
    lastOne: false,
    speedRouteHistory: 1000,
    mtsRoute: null,
    mtsRouteStar: 0.01,
    mtsRouteFix: 0.05,
    mtsStop: 0.025,
    timesStop: 20,
    doesWorkDrawRouteCount: 0,
    origin: null,
    destination: null,
    originClient: null,
    destinationClient: null,
    firstRoute: true,
    firstClientRoute: true,
    firstBusRoute: true,
    doesnWorkDrawRoute: false,
    lastCardinalPoint: "",
    showNotificationsAlert: true,
    showNotifications: false,
    currentSlideRouteNumber: 0,
    slideInterval: null,
    usernameSelected: null,//clientID: null,
    isClear: false,
    route: null,
    timeToCalculateRouteClients: 500,
    clientsRoutesSelectedTableView: null,
    updatePanelObject: null,
    responseEndObject: null,
    btnGenerateRoute: null,
    totallyClients: 0,
    peopleTotalIn: -1,
    peopleTotalOut: -1,
    response: null
};


var MasterPage;
var objMapControl;

jQuery(document).ready(function () {
    try {
        jQuery.noConflict();
    } catch (e) { };
});

function initialize()
{
    try{
        MasterPage = new MasterPage();
        objMapControl = new MapControl();
        getInfoCompany();
    }
    catch(ex){
        nul;
    }
}

function getInfoCompany() {
    PageMethods.wsGetCompanyInfoByCompanyID(getInfoCompanyOk, getInfoCompanyError);
}

function getInfoCompanyOk(object) {
    MasterPage.openXMLCompanyInfo(object);
    objMapControl.initializeMap();
    ShowGpsImei();
    globalMasterPage.intervalGPSImei = setInterval("getImeiDataTrackLastLocationByUser()", 2000);
}

function getInfoCompanyError(error) {
    objWindowsMessageManager.show_Warning(error.get_message(), "Company");
}

function getImeiDataTrackLastLocationByUser() {
    PageMethods.wsGetImeiTrackDataLastLocationByUser(getImeiDataTrackLastLocationByUserOk, getImeiDataTrackLastLocationByUserError);
}

function getImeiDataTrackLastLocationByUserOk(xmlObject) {
    MasterPage.openXMLTrackDataLastLocationImei(globalGPSImei.imeiSelected, objMapControl, xmlObject, globalMasterPage.trackDataLastLocationList);

    //var myTrackDataLastLocationImei = new TrackDataLastLocationImei();

    //myTrackDataLastLocationImei.showNotification('notification', 'imageLocation', 'titleNotification');

    var index = MasterPage.getIndexImei(globalMasterPage.trackDataLastLocationList, globalGPSImei.imeiSelected);
    if (index != -1) {
        setInfoReal(index);
    }

    if (globalMasterPage.intervalGPSImei != null) {
        clearInterval(globalMasterPage.intervalGPSImei);
        globalMasterPage.intervalGPSImei = null;
        setInterval("getImeiDataTrackLastLocationByUser()", 15000);
    }

}

function getImeiDataTrackLastLocationByUserError(error) {
    //msgError(error.get_message(), titles.polygonoTitle);
}
